﻿using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Tokens
{
    internal struct ColRelNegU
    {
        public override string ToString()
        {
            return " col " + col + " rowRelative " + rowRelative + " columnRelative " + columnRelative;
        }
        private const ushort relativeColumnMask = 0x4000;
        private const ushort relativeRowMask = 0x8000;
        public short col;
        public bool columnRelative;
        public bool rowRelative;

        public ColRelNegU(short columnIndex, bool isRelativeRow, bool isRelativeColumn)
        {
            this.col =  columnIndex;
            this.columnRelative = isRelativeColumn;
            this.rowRelative = isRelativeRow;
        }

        public void Read(BinaryReader reader)
        {
            ushort colAb = reader.ReadUInt16();
            col = (short) (colAb & 0x3FFF);
            columnRelative = (colAb & relativeColumnMask) != 0;
            rowRelative = (colAb & relativeRowMask) != 0;
        }

        public void Write(BinaryWriter writer)
        {
            var colAb = (ushort) col;
            if (columnRelative)
                colAb |= relativeColumnMask;
            if (rowRelative)
                colAb |= relativeRowMask;
            writer.Write(colAb);
        }
    }
}